package your.calc1;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class Calc1Activity extends Activity {
	/** Called when the activity is first created. */
	private static final int REQUEST_CODE = 1234;
	private ListView wordsList;
	CharSequence text = "";
	CharSequence math = "";
	int result = 0;
	boolean enter = false;
 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Button speakButton = (Button) findViewById(R.id.speakButton);

		// Disable button if no recognition service is present
		PackageManager pm = getPackageManager();
		List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(
				RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
		if (activities.size() == 0) {
			speakButton.setEnabled(false);
		}
		setContentView(R.layout.main);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return true;
	}

	/**
	 * Handle the action of the button being clicked
	 */
	public void speakButtonClicked(View v) {
		startVoiceRecognitionActivity();
	}

	/**
	 * Fire an intent to start the voice recognition activity.
	 */
	private void startVoiceRecognitionActivity() {
		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
				RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice recognition...");
		startActivityForResult(intent, REQUEST_CODE);
	}

	/**
	 * Handle the results from the voice recognition activity.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
			// Populate the wordsList with the String values the recognition
			// engine thought it heard
			resetData();
			ArrayList<String> matches = data
					.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			TextView tv = (TextView) this.findViewById(R.id.editText1);
			tv.setText(matches.get(0).toString().replace(" ", ""));
			recognizeText(tv.getText());
			result = Calculate();
			recognizeText(tv.getText());
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	public void recognizeText(CharSequence text) {

		TextView tv = (TextView) this.findViewById(R.id.editText1);
		if (enter == true) {
			tv.setText(text + " = " + result);
		} else {
			tv.setText(text);
			if (text.toString().indexOf("+") > 0) {
				math = "+";
			}
			if (text.toString().indexOf("-") > 0) {
				math = "-";
			}
			if (text.toString().indexOf("*") > 0) {
				math = "*";
			}
			if (text.toString().indexOf("/") > 0) {
				math = "/";
			}

		}

	}

	public void resetData() {
		text = "";
		math = "";
		result = 0;
		enter = false;
	}

	public void onClick(View target) {

		switch (target.getId()) {
		case R.id.buttonX:
			resetData();
			break;

		default:
			break;
		}
		if (enter == true) {
			resetData();
		}
		if (enter == false) {
			switch (target.getId()) {
			case R.id.Button01:
				text = text + "1";
				break;
			case R.id.Button02:
				text = text + "2";
				break;
			case R.id.Button03:
				text = text + "3";
				break;
			case R.id.Button04:
				text = text + "4";
				break;
			case R.id.Button05:
				text = text + "5";
				break;
			case R.id.Button06:
				text = text + "6";
				break;
			case R.id.Button07:
				text = text + "7";
				break;
			case R.id.Button08:
				text = text + "8";
				break;
			case R.id.Button09:
				text = text + "9";
				break;
			case R.id.Button00:
				text = text + "0";
				break;
			case R.id.ButtonP:
				if (math == "") {
					math = "+";
					text = text + "" + math;
				}
				break;
			case R.id.ButtonM:
				if (math == "") {
					math = "-";
					text = text + "" + math;
				}
				break;
			case R.id.ButtonU:
				if (math == "") {
					math = "*";
					text = text + "" + math;
				}
				break;
			case R.id.ButtonD:
				if (math == "") {
					math = "/";
					text = text + "" + math;
				}
				break;
			case R.id.buttonX:
				resetData();
				break;
			case R.id.ButtonEnter:
				result = Calculate();
				break;

			default:
				break;
			}
		}
		recognizeText(text);
	}

	private int Calculate() {
		enter = true;
		TextView tv = (TextView) this.findViewById(R.id.editText1);
		text = tv.getText();
		try {
			int Calc1 = Integer.parseInt(text.toString().substring(0,
					text.toString().indexOf(math.toString())));
			int Calc2 = Integer.parseInt(text.toString().substring(
					text.toString().indexOf(math.toString()) + 1));
			if (math == "+") {
				return Calc1 + Calc2;
			}
			if (math == "-") {
				return Calc1 - Calc2;
			}
			if (math == "*") {
				return Calc1 * Calc2;
			}
			if (math == "/" && Calc2 != 0) {
				return Calc1 / Calc2;
			}
		} catch (Exception e) {
			showDialog(0);
		}

		return 0;
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		DialogInterface.OnClickListener doNothing = new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				resetData();
			}
		};
		int alertMessage;

		switch (id) {
		case 0:
			alertMessage = R.string.error;
			break;
		default:
			return null;
		}
		return new AlertDialog.Builder(this).setMessage(alertMessage)
				.setNeutralButton(R.string.ok, doNothing).create();
	}
}
